package com.sxfq.loansupermarket.backsystem.modular.theTest.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.theTest.dao.TheTestInfoDao;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.SearchTheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.ITheTestInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据统计Dao
 *
 * @author fengshuonan
 * @Date 2017-07-19 16:23:35
 */
@Service
public class TheTestInfoServiceImpl implements ITheTestInfoService {

    @Autowired
    private TheTestInfoDao theTestInfoDao;

    @Autowired
    private SqlMapper sqlMapper;


    @Override
    public List<TheTestInfo> selectPageListByQueryGetMap(Page<TheTestInfo> page, SearchTheTestInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getTheTestName())) {
            params.put("theTestName", searchInfo.getTheTestName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        return theTestInfoDao.getQueryPage(page, params);
    }

    public TheTestInfo selectOneById(String theTestId) {
        String sql = "select * from the_test_info where id= '" + theTestId + "'";
        TheTestInfo theTestInfo = sqlMapper.selectOne(sql, TheTestInfo.class);
        return theTestInfo;
    }

    public int insert(TheTestInfo info) {
        String id = CommUtils.getUUID();
        String sql = "INSERT INTO `the_test_info` (`id`, `scores_of_adoption`, `the_test_name`, `type_of_question`, `number_of_question`, `scores_of_question`, `create_time`, `update_time`, `update_people`, `create_people`, `time_length`, `remark`, `pass_word`, `total_score`) VALUES" +
                "('" + id + "'," + info.getScoresOfAdoption() + ",'" + info.getTheTestName().trim() + "'," + info.getTypeOfQuestion() + "," + info.getNumberOfQuestion() + "," + info.getScoresOfQuestion() + ",'" + DateTimeKit.now() + "'," +
                "'" + DateTimeKit.now() + "','" + ShiroKit.getUser().getName() + "','" + ShiroKit.getUser().getName() + "','" + info.getTimeLength() + "','" + info.getRemark() + "','" + info.getPassWord().trim() + "'," + info.getNumberOfQuestion() * info.getScoresOfQuestion() + ");";
        return sqlMapper.insert(sql);
    }

    public int update(TheTestInfo info) {
        info.setTotalScore(info.getNumberOfQuestion()*info.getScoresOfQuestion());
        return theTestInfoDao.updateById(info);
    }

    @Transactional
    public int selectSameTestNameCount(String theTestName) throws Exception {
        int count = theTestInfoDao.selectSameTestNameCount(theTestName);
        return count;
    }

    @Transactional
    public int selectSameTestNameCountForUpdate(String id, String theTestName) throws Exception {
        int count = theTestInfoDao.selectSameTestNameCountForUpdate(id, theTestName);
        return count;
    }

    @Transactional
    public void delete(String id) {
        String sql = "delete from the_test_info  where id = '" + id + "'";
        sqlMapper.delete(sql);
    }
}
